import Line from '../elements/line';
import Text from '../elements/text';
import * as _ from '../utils';
const xAxisAction = function (xAxis, origin) {
    let show = _.getObjProperty(xAxis, 'show', true);
    let elements = [];
    let xOrigin = {};

    let data = xAxis.data;
    let xAxisLen = data.length;
    let yAxisWidth = xAxis.yAxisWidth;

    let x = origin.x;
    let y = origin.y;
    let w = origin.w;
    let h = origin.h;

    let p_x = x + yAxisWidth;
    let p_y = y + h;
    let xLen = parseInt((w - yAxisWidth) / xAxisLen);

    let e_x = p_x + xLen * xAxisLen;
    let e_y = y + h;

    let lineStyle = {
        strokeStyle: '#000000',
        zIndex: 1000,
    };

    let xLine = new Line({
        points: [
            {
                x: p_x,
                y: p_y,
            },
            {
                x: e_x,
                y: e_y,
            },
        ],
        style: lineStyle,
    });
    show && elements.push(xLine);

    for (let i = 0; i < xAxisLen + 1; i++) {
        if (i < xAxisLen) {
            let text = data[i];
            let textStyle = {
                text: text,
                fillStyle: '#333333',
                textAlign: 'center',
                fontSize: 12,
            };
            let textPoints = [
                {
                    x: p_x + xLen * (i + 0.5),
                    y: p_y + 14,
                },
            ];
            let yText = new Text({
                points: textPoints,
                style: textStyle,
            });
            show && elements.push(yText);
        }
        let points = [
            {
                x: p_x + xLen * i,
                y: p_y,
            },
            {
                x: p_x + xLen * i,
                y: p_y + 5,
            },
        ];

        let scaleLine = new Line({
            points,
            style: lineStyle,
        });
        show && elements.push(scaleLine);
    }

    return {
        elements,
        xOrigin,
    };
};
export default xAxisAction;
